import { Component, OnInit } from '@angular/core';
import { AxiosService } from '../../axios.service';

@Component({
  selector: 'app-auto-paiban',
  templateUrl: './auto-paiban.component.html',
  styleUrls: ['./auto-paiban.component.scss']
})
export class AutoPaibanComponent implements OnInit {
  listdata;//上边展示排班表数据
  riqi = [];
  datelist = [];//下边展示的详细信息
  isshow = {};//是否展示
  hfsc;
  hfrq;
  isVisible = false;//是否删除
  shanchuname;
  dqri;
  unionorder;
  one1 = 0;
  one2 = 0;
  one3 = 0;
  one4 = 0;
  today;
  constructor(
    public axios: AxiosService,
  ) { }

  ngOnInit(): void {
    this.getalldata();
  }
  getalldata() {
    this.axios.axiosPost("QueryTasktable").then((data) => {
      console.log(data)
      this.riqi = [];
      if (data["result"] == "ok") {
        this.listdata = data["order"]
        this.today = data["today"]
        console.log(this.datelist)
        this.one1 = 0;
        this.one2 = 0;
        this.one3 = 0;
        this.one4 = 0;
        var a = 0;
        for (var i = 0; i < this.listdata.length; i++) {
          this.riqi.push(this.listdata[i].date.split("-"));
          if (a == 0) {
            console.log(this.listdata[i].design.estimate_hour)
            this.one1 += Number(this.listdata[i].design.estimate_hour)
            this.one2 += this.listdata[i].jiagong.estimate_hour
            this.one3 += this.listdata[i].ercijiagong.estimate_hour
            this.one4 += this.listdata[i].zuzhuang.estimate_hour
          }
          if (this.listdata[i].date == this.today) {
            a++
          }

        }

      }
      console.log(this.riqi)
      console.log(this.listdata)
    })
  }
  list(item, key, tsck) {
    for (var i = 0; i < this.listdata.length; i++) {
      this.listdata[i].design.hit = false;
      this.listdata[i].code.hit = false;
      this.listdata[i].jiagong.hit = false;
      this.listdata[i].ercijiagong.hit = false;
      this.listdata[i].zuzhuang.hit = false;

    }
    if (tsck == 0) {
      this.listdata[key].design.hit = true;
    } else if (tsck == 1) {
      this.listdata[key].code.hit = true;
    } else if (tsck == 2) {
      this.listdata[key].jiagong.hit = true;
    } else if (tsck == 3) {
      this.listdata[key].ercijiagong.hit = true;
    } else if (tsck == 4) {
      this.listdata[key].zuzhuang.hit = true;
    }
    this.datelist = item;
    for (let i = 0; i < this.datelist.length; i++) {
      this.isshow[i] = { edit: true, isdel: true, data: this.datelist[i] };
    }
  }



  // 修改信息
  startEdit(key: number): void {
    console.log(key)
    console.log(this.isshow[key])
    this.isshow[key].isdel = false;
    this.isshow[key].edit = false;
    this.hfrq = this.isshow[key]["data"].date
    this.hfsc = this.isshow[key]["data"].workhour
    this.dqri = this.isshow[key]["data"].date;
    this.unionorder = this.isshow[key]["data"].union_order;

  }
  // 保存修改
  saveEdit(key: number): void {
    this.isshow[key].isdel = true;
    this.isshow[key].edit = true;
    console.log(String(this.isshow[key]["data"].workhour).indexOf("."));
    // String(this.isshow[key]["data"].workhour)

    if (String(this.isshow[key]["data"].workhour).indexOf(".")) {

      this.isshow[key]["data"].workhour = Number(String(this.isshow[key]["data"].workhour).substring(0, String(this.isshow[key]["data"].workhour).indexOf(".") + 2))
    }
    // console.log(this.isshow[key]["data"].name, this.dqri, this.isshow[key]["data"].task)
    // console.log(this.isshow[key]["data"].date, this.isshow[key]["data"].workhour, this.unionorder)
    this.axios.axiosPost("UpdateTask", {
      "name": this.isshow[key]["data"].name, "date": this.dqri,
      "task": this.isshow[key]["data"].task, "dateNew": this.isshow[key]["data"].date, "workhour": this.isshow[key]["data"].workhour, unionorder: this.unionorder
    }).then((date) => {
      console.log(date)
      this.getalldata();
    })

  }
  // 撤销修改
  cancelEdit(key: number): void {
    this.isshow[key].isdel = true;
    this.isshow[key].edit = true;
    this.isshow[key]["data"].date = this.hfrq
    this.isshow[key]["data"].workhour = this.hfsc
  }
  showModal(item) {
    console.log(item)
    this.shanchuname = item.name;
    this.isVisible = true;
    this.unionorder = item.union_order
  }
  handleCancel() {
    this.isVisible = false;
  }
  handleOk() {
    this.isVisible = false;
    this.axios.axiosPost("DeleteTask", {
      "name": this.shanchuname, unionorder: this.unionorder
    }).then((date) => {
      console.log(date)
      this.getalldata();
    })
  }
}
